package com.mvc.business.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.extremecomponents.table.limit.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.base.service.SupplierManager;
import com.mvc.business.model.Schedule;
import com.mvc.business.service.ScheduleManager;
import com.mvc.framework.dao.PropertyFilter;
import com.mvc.framework.model.BaseEntity;
import com.mvc.framework.service.PageManager;
import com.mvc.framework.util.PageSearch;
import com.mvc.framework.util.PageUtils;
import com.mvc.framework.web.PageController;

/**
 * 
 * @author snowpbx
 * @version V1.0 Create Time: 2012-12-2
 */
@Controller
@RequestMapping("schedule")
public class ScheduleController extends PageController<Schedule> {

	@Autowired
	private ScheduleManager	scheduleManager;

	@Autowired
	private SupplierManager	supplierManager;

	@RequestMapping("finished")
	public String finished(HttpServletRequest request) {
		request.setAttribute("action", "finished");
		Limit limit = PageUtils.getLimit(request);
		PageSearch page = preparePage(request, limit);
		page.getFilters().add(new PropertyFilter("EQB_finished", Boolean.TRUE + ""));
		super.handlePage(request, page);
		afterPage(request, page, IS_NOT_BACK);
		request.setAttribute("suppliers", supplierManager.getAll());
		return getFileBasePath() + "listSchedule";
	}

	@RequestMapping("finish/{scheduleId}")
	public String finished(ModelMap modelMap, @PathVariable Integer scheduleId) {
		Schedule schedule = scheduleManager.getByObjectId(scheduleId);
		boolean result = false;
		if (schedule != null && !schedule.getFinished()) {
			schedule.setFinished(Boolean.TRUE);
			scheduleManager.save(schedule);
			result = true;
		}
		modelMap.addAttribute("result", result);
		return "jsonView";
	}

	@RequestMapping("my")
	public String my(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Limit limit = PageUtils.getLimit(request);
		PageSearch page = preparePage(request, limit);
		if (request.getSession().getAttribute("supplierId") != null) {
			page.getFilters().add(new PropertyFilter("EQI_supplierId", request.getSession().getAttribute("supplierId").toString()));
		}
		pageSetting(request, page);
		scheduleManager.find(page, true);
		afterPage(request, page, IS_NOT_BACK);
		request.setAttribute("action", "my");
		return getFileBasePath() + "listSchedule";
	}

	protected String handlePage(HttpServletRequest request, PageSearch page) {
		pageSetting(request, page);
		return super.handlePage(request, page);
	}

	protected void pageSetting(HttpServletRequest request, PageSearch page) {
		request.setAttribute("action", "page");
		page.getFilters().add(new PropertyFilter("EQB_finished", Boolean.FALSE + ""));
		request.setAttribute("suppliers", supplierManager.getAll());
	}

	protected String handleSave(HttpServletRequest request, ModelMap modelMap, Schedule order) throws Exception {
		if (order.getGenerateDate() == null) {
			order.setGenerateDate(new Date());
		}
		getEntityManager().save(order);
		return "redirect:edit/" + ((BaseEntity) order).getId() + getMessage("common.base.success", request) + "&action="
		        + request.getParameter("action");
	}

	@RequestMapping("batchUpdate/{purchaseId}/{type}")
	public String batchUpdate(@PathVariable Integer type, Date setting) {

		return "jsonView";
	}

	@Override
	public PageManager<Schedule> getEntityManager() {
		return scheduleManager;
	}

	@Override
	public String getFileBasePath() {
		return "business/";
	}

}
